#ifndef PARSER_H
#define PARSER_H

#include "xercesc/dom/DOM.hpp"
#include "xercesc/util/BinMemInputStream.hpp"
#include "xercesc/framework/Wrapper4InputSource.hpp"
#include "xercesc/framework/MemBufInputSource.hpp"
#include "XmlUtil.h"
#include <assert.h>
#include <iostream>
#include "../jncSingleton.h"

namespace xerces {


class XmlData : public DOMInputSource {
 public :
  XmlData(const XMLCh* enc, const XMLCh* pId, const XMLCh* sId, const XMLCh* base = NULL) 
    : DOMInputSource() 
    ,encoding(enc)
    ,publicId(pId)
    ,systemId(sId)
    ,baseURI(base)
    , buf(NULL) 
    , IssueFatalError(true) {

      buf = new BinMemInputStream((XMLByte*)XtoStr(systemId), strlen(XtoStr(systemId)));

    } 
    ~XmlData() {
	release();
      }
    const XMLCh* getEncoding() const {
      return encoding;
    }
    
    const XMLCh* getPublicId() const {
      return publicId;
    }
    
    const XMLCh* getSystemId() const {
      return systemId;
    }
    
    const XMLCh* getBaseURI() const {
      return baseURI;
    }
    
    void setEncoding(const XMLCh* const enc) { 
      encoding = new XMLCh((const XMLCh)*enc);
    }
    
    void setPublicId(const XMLCh* const pId) {
      publicId = new XMLCh((const XMLCh)*pId);
    }
    
    void setSystemId(const XMLCh* const sId) {
      systemId = new XMLCh((const XMLCh)*sId);
    }
    
    void setBaseURI(const XMLCh* const base) {
      baseURI = new XMLCh((const XMLCh)*base);
    }
    
    BinInputStream* makeStream() const {
      return (BinInputStream*)buf;
    }
    
    bool getIssueFatalErrorIfNotFound() const {
      return IssueFatalError;
    }
    
    void setIssueFatalErrorIfNotFound(const bool flag) {
      IssueFatalError = flag;
    }
    
    void release() {
      if(buf)
	delete buf;
    }
 
 private:
    XmlData(const XmlData&);
    const XMLCh* encoding;
    const XMLCh* publicId;
    const XMLCh* systemId;
    const XMLCh* baseURI;
    const BinMemInputStream* buf;
    bool IssueFatalError;
    
  };
  
  DOMBuilder* getXmlParser();
  DOMWriter* getXmlWriter();

}; // end of namespace xerces


#endif
